package mn.slide.laborberlin.main;

import java.util.ArrayList;
import java.util.List;

import mn.slide.laborberlin.adaptor.UeberSchriftAdapter;
import mn.slide.laborberlin.model.EntragData;
import mn.slide.laborberlin.model.SubUeberSchrift;
import mn.slide.laborberlin.model.UeberBase;
import mn.slide.laborberlin.model.UeberSchrift;
import mn.slide.laborberlin.model.UnterSuchung;
import mn.slide.laborberlin.tabgroup.GroupStarter;
import mn.slide.laborberlin.tabgroup.HomeGroupActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class UeberSchriftActivity extends Activity {
	private ListView listV;
	private List<EntragData> item;
	private UeberBase ueber;
	EditText search_edt;

	// private Button back_btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		listV = (ListView) findViewById(R.id.main_list);
		View footerView = ((LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.ein_group_row, null, false);
		ueber = getIntent().getExtras().getParcelable("ueber");
		item = new ArrayList<EntragData>();
		for (UnterSuchung unter : ueber.getUnterSuchungSet()) {
			item.add(unter);
		}
		if (ueber instanceof UeberSchrift)
			for (SubUeberSchrift unter : ((UeberSchrift) ueber)
					.getSubUeberSchriftSet()) {
				item.add(unter);
			}
		search_edt = (EditText) findViewById(R.id.search_edt);
		search_edt.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					String k = search_edt.getText().toString();
					if (k.length() > 0) {

						Intent intent = new Intent(UeberSchriftActivity.this,
								SearchActivity.class);
						intent.putExtra("searchTxt", k);
						HomeGroupActivity.group.goTo(intent);

					}
					return true;
				}
				return false;
			}
		});
		ArrayAdapter<EntragData> adapter = new UeberSchriftAdapter(this, item);
		listV.addHeaderView(footerView);
		listV.setAdapter(adapter);
		TextView title = (TextView) findViewById(R.id.tvGroupName);
		if (ueber.getName() != null)
			title.setText(ueber.getName());
		else
			title.setText(item.get(0).getName());
		ImageView arrow = (ImageView) findViewById(R.id.arrow);
		arrow.setVisibility(View.INVISIBLE);

	}

	@Override
	public void onBackPressed() {

		GroupStarter.getInstance().onBackpressed(this);
	}

}
